//ReadEmd.js
//为显示龙葫芦做的测试程序，不能做通用
  
function ObjColorIndex()
{
	this.m_ObjType = "";
	this.m_ObjColor = 0;
}

function EmdTexInfo()
{
	this.fileName = "";
	this.fileSize = 0;
}

function EmdBuffer()
{ 
	this.m_VertexBuf 		= null;
	this.m_VFVBuf 			= null;
	this.m_IndexBuf 		= null;
	this.m_NormalBuf 		= null;
	this.m_ColorBuf 		= null;	
	this.m_TexBuf 			= null;
	this.m_ObjName 			= null;
	this.m_CountFace 		= 0;
	this.m_CountVertex 		= 0;
	this.m_CountIndex 		= 0;
	this.m_HaveMaterial 	= 0;
	this.m_VertData     	= 0;
	this.m_Ambient 			= new Float32Array(3);
	this.m_Diffuse 			= new Float32Array(3);
	this.m_Specular 		= new Float32Array(3);
	this.m_Shiness 			= 0;
	this.m_HavTex 			= 0;
	this.m_TexName 			= null;
	this.m_TexFileSize 		= 0;
	this.m_TextureID 		= 0;
	this.m_TextureBK 		= 0;
	this.m_HaveFlc 			= 0;
	this.m_MatArray 		= new Array();
	this.SpecPower 			= 0.0;
	this.SelfColorFalg 		= 0;
	this.SelfColor 			= null;
	this.SelfParam 			= 0.0;
	this.m_IsShow			= true;						//如果为false，则不被绘制
	this.m_CanSel			= true;						//是否可以被选择，如果不可以，则DrawSel过程将不绘制该Obj
	this.m_ObjIndex 		= 0;						//ReadEmd加载过的所有OBJ进行统一编号
	this.m_ObjIndexColor 	= new Float32Array(4);		//从m_ObjIndex转换过来
}


function ReadEmd(gl)
{
	this.fileName 			= "";
	this.m_Delay 			= 0;
	this.m_CurFrame   		= 0;
	this.m_CountFrame 		= 0;
	this.m_PlayEndFrame 	= 0;
	this.m_PlayFlag 		= 0;
	this.m_Pos 				= new CVector3(0,0,0);
	this.m_Rotate 			= new CVector3(0,0,0);
	this.m_BufObj 			= new Object();
	this.m_EmdBuf 			= new Array();
	this.m_GL 				= gl;
	this.m_ShaderProg  		= -1;
	this.m_CountObj 		= 0;
	this.m_Complete 		= 0;
	this.m_TexCompFlag 		= null;
	this.m_CountTex			= 0;
	this.m_SyncTool 		= new CSyncTool();
	this.m_GetFile 			= new CGetFile();
	
	this.a_Position 		= -1;
	this.a_Normal 			= -1;
	this.a_Color  			= -1;
	this.a_TexCoor  		= -1;
	this.u_MvpMatrix  		= -1;
	this.u_NormalMatrix  	= -1;
	this.u_NormalMatrix2	= -1;
	this.u_TexSampler  		= -1;
	this.u_NoTexFlag  		= -1;
	this.u_FlcMatrix      	= -1;
	this.u_HaveFlcFlag    	= -1;
	this.u_ModelMatrix		= -1;
	this.u_MViewMatrix 		= -1;
	this.u_Diffuse			= -1;
	this.u_Ambient			= -1;
	this.u_Specular			= -1;
	this.u_EyePos	 		= -1;
	this.u_Shiness	 		= -1;
	this.u_Power			= -1; 
	this.m_AutoRePlay	  	= 0;
	this.m_iNormalSimp		= -1;
	this.m_ShowNormalHadle 	= -1;


	this.m_ShaderOutline	= -1;
	this.m_VertOutline		= -1;
	this.m_TexCoorOutline	= -1;
	this.m_NorOutline		= -1;
	this.m_ColorOutline		= -1;
	this.m_NorMatOutline  	= -1;

	this.m_MvpOutline  		= -1;
	this.m_MVOutline  		= -1;
	this.m_ProjOutline  	= -1;
	this.m_TexSampOutline 	= -1;
	this.m_NoTexOutline 	= -1;
	this.m_DensityHandle	= -1;
	this.m_Density	= 1.0;
	this.m_DenDir			= -1.0;	//闪烁边缘的ALPHA变化系数符号

	//选择用SHADER句柄
	this.m_ShaderSel 		= -1;  
	this.m_VertSel 			= -1;
	this.m_MvpSel 			= -1;
	this.m_ColorSel			= -1;
	this.m_FlcMatrixSel     = -1;
	this.m_HaveFlcFlagSel   = -1;


	this.m_ShaderAABB		= -1;
	this.m_VertAABB 		= -1;
	this.m_MvpAABB			= -1;
	this.m_BufAABB 			= null;
	this.m_IndexAABB 			= null;

	this.m_uColor 			= new Float32Array(4);	
	this.m_modelMatrix 		= new CMatrix4();
	this.m_mvMatrix 		= new CMatrix4();
	this.m_ProjMatrix 		= new CMatrix4();
	this.m_mvpMatrix 		= new CMatrix4();
	this.m_normalMatrix 	= new CMatrix4();


	this.m_SelObjColor 		= new Array();
	this.m_CountSelObj 		= 6;		//一共有多少个可以被选择的物体
	var i;
	for(i = 0;i<6; i++)
	{
		this.m_SelObjColor[i] = new ObjColorIndex();
		this.m_SelObjColor[i].m_ObjColor = 10+i;
	}
	this.m_Pos.x 			= 0.0;
	this.m_Pos.y 			= 0.0;
	this.m_Pos.z 			= 0.0;
	this.m_Scale 			= new CVector3(1,1,1);
	this.m_Direction		= new CVector3(-1,0,0);
	this.m_Product_Count 	= 0;
	this.m_ProductNameArray = new Array();
	this.m_HideNum 			= new Array();
	this.ReadFlag 			= new Int32Array(2);
	this.m_TexPath;
	this.m_CountTex 		= 0;
	this.m_TexBeginPos 		= 0;
	this.m_TexReadPos		= 0;
	this.m_TexInfo 			= new Array(); //EmdTexInfo
	this.m_IndexForSel 		= 0;
	this.m_CompleteCBFunc 	= null;
	this.m_IsShow			= true;						//如果为false，则不被绘制
	this.m_MaxPoint 		= new CVector3(-1000000,-1000000,-1000000);
	this.m_MinPoint 		= new CVector3(1000000,1000000,1000000);
	this.m_CenterPoint 		= new CVector3();
	this.m_NormapTexID		= 0;
	this.m_ShowNormalFlag 	= true;


}

ReadEmd.m_SubObjIndex = 1;			//每读到一个OBJ，自动加1，以此作为该OBJ的索引,每读一个文件，也加1，作为该文件的对应的颜色索引
									//这样，文件的总索引，与子物体的所以不一致，将来寻找的时候，很容易分辨

ReadEmd.prototype.ShowProd = function(index, num)
{
    var i;
    for(i = 0; i<this.m_HideNum[index].length; i++)
    {
        this.m_HideNum[index][i] = 1;
    }
    this.m_HideNum[index][num] = 0;   //这个显示 
}

ReadEmd.prototype.AddProductName = function(name)
{
    var count = this.m_Product_Count;
    this.m_ProductNameArray[count] = new Array();
    this.m_HideNum[count] = new Array();
    for(i = 0; i<name.length; i++)
    {
        this.m_ProductNameArray[count][i] = ""+name[i];
        this.m_HideNum[count][i] = 1;
    }
    this.m_HideNum[count][0] = 0;       //默认第0个物品是不隐藏的
    this.m_Product_Count ++;
}

ReadEmd.prototype.InitShader = function()
{
	var gl = this.m_GL;
	var shader = new CEmdShader();
	shader.InitShader();
	this.m_ShaderProg = shader.GetShader();
	gl.useProgram(this.m_ShaderProg);
	var program = this.m_ShaderProg;
	this.a_Position 	= gl.getAttribLocation(program, 'a_Position');
	this.a_Normal 		= gl.getAttribLocation(program, 'a_Normal');
	this.a_Color 		= gl.getAttribLocation(program, 'a_Color');
	this.a_TexCoor 		= gl.getAttribLocation(program, 'a_TexCoord');
	this.u_NoTexFlag 	= gl.getUniformLocation(program, 'u_NoTexFlag');
	this.m_ShowNormalHadle 	= gl.getUniformLocation(program, 'u_ShowNorFlag');
	this.u_MvpMatrix 	= gl.getUniformLocation(program, 'u_MvpMatrix');
	this.u_FlcMatrix 	= gl.getUniformLocation(program, 'u_FlcMatrix');
	this.u_NormalMatrix = gl.getUniformLocation(program, 'NormalMatrix');
	this.u_NormalMatrix2= gl.getUniformLocation(program, 'NormalMatrix2');
	this.u_ModelMatrix 	= gl.getUniformLocation(program, 'ModelMatrix');
	this.u_MViewMatrix 	= gl.getUniformLocation(program, 'MVMatrix');
	//alert(""+this.u_MViewMatrix);
	this.u_TexSampler 	= gl.getUniformLocation(program, 'u_Sampler');	
	this.m_iNormalSimp	= gl.getUniformLocation(program, 'u_NormalSampler');
	this.u_HaveFlcFlag 	= gl.getUniformLocation(program, 'u_HaveFlc');
	this.u_Diffuse 		= gl.getUniformLocation(program, 'Diffuse');
	this.u_Ambient 		= gl.getUniformLocation(program, 'Ambient');
	this.u_Specular	 	= gl.getUniformLocation(program, 'Specular');

	this.u_EyePos	 	= gl.getUniformLocation(program, 'eyepos');
	this.u_Shiness	 	= gl.getUniformLocation(program, 'shiness');
	this.u_Power	 	= gl.getUniformLocation(program, 'power');




	this.m_ShaderOutline = shader.GetOutLineShader();
	gl.useProgram(this.m_ShaderOutline);

	var progout = this.m_ShaderOutline;
	this.m_VertOutline 			= gl.getAttribLocation(progout, 'a_Position');
	this.m_NorOutline 			= gl.getAttribLocation(progout, 'a_Normal');
	this.m_TexCoorOutline 		= gl.getAttribLocation(progout, 'a_TexCoord');
	this.m_MvpOutline 			= gl.getUniformLocation(progout, 'u_MvpMatrix');
	this.m_NorMatOutline	 	= gl.getUniformLocation(progout, 'u_NormalMatrix');
	this.a_Color 				= gl.getUniformLocation(progout, 'a_Color');
	this.m_TexSampOutline 		= gl.getUniformLocation(progout, 'u_Sampler');
	//this.m_NoTexOutline 		= gl.getUniformLocation(program, 'u_NoTexFlag');
	this.m_ModeOutline			= gl.getUniformLocation(progout, 'u_ModelMatrix');
	this.m_MVOutline  			= gl.getUniformLocation(progout, 'u_MvMatrix');
	this.m_ProjOutline  		= gl.getUniformLocation(progout, 'u_ProjMatrix');
	this.m_DensityHandle = gl.getUniformLocation(progout, 'density');

	this.m_ShaderSel = shader.GetSelShader();
	gl.useProgram(this.m_ShaderSel);
	var progsel = this.m_ShaderSel;

	this.m_VertSel 			= gl.getAttribLocation(progsel, 'a_Position');
	this.m_MvpSel 			= gl.getUniformLocation(progsel, 'u_MvpMatrix');
	this.m_ColorSel 		= gl.getUniformLocation(progsel, 'u_Color');
	this.m_FlcMatrixSel     = gl.getUniformLocation(progsel, 'u_FlcMatrix');
	this.m_HaveFlcFlagSel   = gl.getUniformLocation(progsel, 'u_HaveFlc');
	this.m_uColor[1] = 0.0;
	this.m_uColor[2] = 0.0;
	this.m_uColor[3] = 1.0;



	this.m_ShaderAABB	= shader.GetAABBShader();
	this.m_VertAABB 	= gl.getUniformLocation(this.m_ShaderAABB, 'a_Position');;
	this.m_MvpAABB		= gl.getUniformLocation(this.m_ShaderAABB, 'u_MvpMatrix');;
}

ReadEmd.prototype.GetTexDataSize = function(name)
{
	var size = 0;
	for(i = 0; i<this.m_CountTex; i++)
	{
		if(name === this.m_TexInfo[i].fileName)
		{
			size = this.m_TexInfo[i].fileSize;
			break;
		}
	}
	return size;
}

ReadEmd.prototype.IsPng = function(name)
{
	var s;
	var len = name.length;
	s = ""+name.substring(len - 3);
	var s2 = s.toLowerCase();
	if(s2 === "png")
	{
		return true;
	}
	else
	{
		return false;
	}
}

//解析文件内容
ReadEmd.prototype.CallBack = function(fileResponse)
{
	var i;
	var gl = this.m_GL;
	var TexPath = this.m_TexPath;
	var program =  this.m_ShaderProg;

	var PicReader = new CLoadPic(gl);
	var MemPicReader = new CLoadMemPic();
	var version,type,resbuf,CountObj;
	var tFile = new CMemoryFile();
	tFile.SetFileData(fileResponse);


	version = tFile.ReadFloat();    		//版本信息;
	type = tFile.ReadInt();         		//纹理资源处理方式
	resbuf = tFile.ReadString(64);     		//纹理资源包名字（只有type为3的时候才有意义）
	this.m_CountObj = tFile.ReadInt();		//当前模型的总模型数;
	this.m_CountFrame = tFile.ReadInt();	//当前模型的总帧数

	
	if(type === 2)							//文件包含了纹理
	{
		this.m_CountTex = tFile.ReadInt();
		this.m_TexBeginPos = tFile.ReadInt();
		this.m_TexReadPos = this.m_TexBeginPos;
		for(i = 0; i<this.m_CountTex; i++)
		{
			this.m_TexInfo[i] = new EmdTexInfo();
			this.m_TexInfo[i].fileName = ""+ tFile.ReadString(64);
			this.m_TexInfo[i].fileSize = tFile.ReadInt();
		}
	}

	CountObj = this.m_CountObj;
	
	var CountVertex;
	var CountIndex;

	this.m_EmdBuf = new Array(CountObj);

	this.m_TexCompFlag = new Int32Array(CountObj);
	
	for(i = 0; i< CountObj; i++)
	{
		this.m_TexCompFlag[i] = 0;
	}
	this.m_CountTex	=	0;		//刚开始的时候，纹理有0个
	var tmpMat = new Float32Array(16);


	PicReader.LoadPic("./Model/Img/NormalsMap.jpg", this.m_NormapTexID, this.m_SyncTool);

	for(i = 0;i < CountObj; i++)
	{
		this.m_EmdBuf[i] = new EmdBuffer();
		this.m_EmdBuf[i].m_HaveFlc = tFile.ReadInt();		//是否有动画
		if(this.m_EmdBuf[i].m_HaveFlc === 1)
		{
			for(mm = 0; mm<this.m_CountFrame; mm++)
			{
				for(nn = 0; nn <16; nn++)
				{
					tmpMat[nn] = tFile.ReadFloat();
				}
        		this.m_EmdBuf[i].m_MatArray[mm] = new CMatrix4(tmpMat);
			}
		}
		var oname = tFile.ReadString(64);
		this.m_EmdBuf[i].m_ObjName = oname;


		var objn = this.m_EmdBuf[i].m_ObjName.substr(0,6);
		{
			if(objn === "CliBox")
			{
				this.m_EmdBuf[i].m_IsShow = false;
			}
		}

	
		

		this.m_EmdBuf[i].m_ObjIndex = 0+ReadEmd.m_SubObjIndex;
		IndexToColor(ReadEmd.m_SubObjIndex, this.m_EmdBuf[i].m_ObjIndexColor);		//将索引号转换为颜色，将来用来被选择
															//OBJ计数器+1

		
		ReadEmd.m_SubObjIndex ++;
		

		this.m_EmdBuf[i].m_HaveMaterial = tFile.ReadInt();

		if(this.m_EmdBuf[i].m_HaveMaterial !== 0)		// 如果这里数据不正常，查询原始数据
		{
		    //this.m_EmdBuf[i].m_Ambient = new Array(3);
		    this.m_EmdBuf[i].m_Ambient[0] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Ambient[1] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Ambient[2] = tFile.ReadFloat();

		    //this.m_EmdBuf[i].m_Diffuse = new Array(3);
		    this.m_EmdBuf[i].m_Diffuse[0] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Diffuse[1] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Diffuse[2] = tFile.ReadFloat();

		    //this.m_EmdBuf[i].m_Specular = new Array(3);
		    this.m_EmdBuf[i].m_Specular[0] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Specular[1] = tFile.ReadFloat();
		    this.m_EmdBuf[i].m_Specular[2] = tFile.ReadFloat();

		    this.m_EmdBuf[i].m_Shiness = tFile.ReadFloat();
		    this.m_EmdBuf[i].SpecPower = tFile.ReadFloat();
			this.m_EmdBuf[i].SelfColorFalg = tFile.ReadInt();
			if(this.m_EmdBuf[i].SelfColorFalg === 1)
			{
				this.m_EmdBuf[i].SelfParam = tFile.ReadFloat();
				this.m_EmdBuf[i].SelfColor = new Float32Array(3);
				this.m_EmdBuf[i].SelfColor[0] = tFile.ReadFloat();
				this.m_EmdBuf[i].SelfColor[1] = tFile.ReadFloat();
				this.m_EmdBuf[i].SelfColor[2] = tFile.ReadFloat();
			}



		    this.m_EmdBuf[i].m_HaveTex = tFile.ReadInt();
		    if(this.m_EmdBuf[i].m_HaveTex === 1)
		    {
  				this.m_CountTex ++;		//增加一个纹理计数
  				this.m_EmdBuf[i].m_TexName = tFile.ReadString(64);
  				this.m_EmdBuf[i].m_TextureID = gl.createTexture();
  				this.m_EmdBuf[i].m_TextureBK = this.m_EmdBuf[i].m_TextureID;	//可能存在着纹理需要替换的情况（例如播放视频），所以需要记录原始纹理
  				if(type === 2)		//模型需要的纹理已经附加在文件里
  				{
  					//这个方式，只能适用于所有的模型都有纹理的情况，否则会出现纹理信息错位
  					var TexData = tFile.GetBlockData(this.m_TexReadPos, this.m_TexInfo[i].fileSize);
  					
  					var exttype = "jpg";
  					if(this.IsPng(this.m_EmdBuf[i].m_TexName))
  					{
  						exttype = "png";
  					}
  					MemPicReader.LoadPic(exttype, this.m_EmdBuf[i].m_TexName, TexData, this.m_EmdBuf[i].m_TextureID, this.m_SyncTool);
  					this.m_TexReadPos = this.m_TexReadPos + this.m_TexInfo[i].fileSize;
  				}
  				else
  				{

  					var FileName = TexPath + this.m_EmdBuf[i].m_TexName;
  	       			PicReader.LoadPic(FileName, this.m_EmdBuf[i].m_TextureID, this.m_SyncTool);
  	       		}
		    }
		}
		else
		{
			this.m_EmdBuf[i].m_HaveTex = 0;
		}
  	
   	
	  	this.m_EmdBuf[i].m_CountVertex = tFile.ReadInt();
	  	this.m_EmdBuf[i].m_CountFace   = tFile.ReadInt();
  	
  		
		var strided = 14;
		var bufsize = strided * this.m_EmdBuf[i].m_CountVertex;

		var TmpBuff = new Float32Array(bufsize);		//Float32Array?

		var j;
		var pos = 0;	//缓冲区位置指针，注意只能再必要的地方进行修改
		var p5 = 0;
		//读顶点数据


		var TmpSize = this.m_EmdBuf[i].m_CountVertex * 3 * 4;
		var Tmp = tFile.GetData(TmpSize);
		var TmpBuffVert = new Float32Array(Tmp);

		this.m_EmdBuf[i].m_VertData = new Float32Array(TmpBuffVert.length);
   
    
      
	  	var ts = this.m_EmdBuf[i].m_CountVertex * 3;
	  	for(m = 0; m< ts ; m++)
	  	{
			this.m_EmdBuf[i].m_VertData[m] = TmpBuffVert[m];
			//求得最大xyz和最小xyz,以求对称中心和AABB包围盒数据

			if((m%3) === 0)
			{
				if(this.m_EmdBuf[i].m_VertData[m] > this.m_MaxPoint.x)
				{
					this.m_MaxPoint.x = this.m_EmdBuf[i].m_VertData[m];
				}

				if(this.m_EmdBuf[i].m_VertData[m] < this.m_MinPoint.x)
				{
					this.m_MinPoint.x = this.m_EmdBuf[i].m_VertData[m];
				}
			}
			if((m%3) === 1)
			{
				if(this.m_EmdBuf[i].m_VertData[m] > this.m_MaxPoint.y)
				{
					this.m_MaxPoint.y = this.m_EmdBuf[i].m_VertData[m];
				}

				if(this.m_EmdBuf[i].m_VertData[m] < this.m_MinPoint.y)
				{
					this.m_MinPoint.y = this.m_EmdBuf[i].m_VertData[m];
				}
			}

			if((m%3) === 2)
			{
				if(this.m_EmdBuf[i].m_VertData[m] > this.m_MaxPoint.z)
				{
					this.m_MaxPoint.z = this.m_EmdBuf[i].m_VertData[m];
				}

				if(this.m_EmdBuf[i].m_VertData[m] < this.m_MinPoint.z)
				{
					this.m_MinPoint.z = this.m_EmdBuf[i].m_VertData[m];
				}
			}
	  	}
	  	

		//读法向量数据
		//Tmp = fileResponse.slice(ReadPos[0], ReadPos[0] + TmpSize);
		Tmp = tFile.GetData(TmpSize);
		var TmpBuffNorm = new Float32Array(Tmp);

		var TmpBuffTex;
		var TmpBuffColor;
		var IndexBuff;
		if( this.m_EmdBuf[i].m_HaveTex === 1)
		{
			//读纹理数据
			TmpSize = this.m_EmdBuf[i].m_CountVertex * 2 * 4;
			Tmp = tFile.GetData(TmpSize);
			var mmm;

			TmpBuffTex = new Float32Array(Tmp);
			var tsize = this.m_EmdBuf[i].m_CountVertex *2;
			for(mmm = 0; mmm<tsize; mmm++)
			{
				TmpBuffTex[mmm] = TmpBuffTex[mmm];
			}
		}
	  	else
	  	{
			//读颜色数据（没有纹理时只有颜色数据）
			//压缩格式的EMD，颜色按照UNSIGNED CHAR 格式存储
			TmpSize = this.m_EmdBuf[i].m_CountVertex * 3 ;
			Tmp = tFile.GetData(TmpSize);
			var tmp2 = new Uint8Array(Tmp);
			TmpBuffColor = new Float32Array(TmpSize);
			for(j = 0; j< this.m_EmdBuf[i].m_CountVertex*3; j++)
			{
				TmpBuffColor[j * 3 + 0] = tmp2[j*3]/255.0;
				TmpBuffColor[j * 3 + 1] = tmp2[j*3]/255.0;
				TmpBuffColor[j * 3 + 2] = tmp2[j*3]/255.0;
			}
		}
		//压缩版的EMD直接存储的SHORT类型索引
		this.m_EmdBuf[i].m_CountIndex = this.m_EmdBuf[i].m_CountFace * 3;

		TmpSize = this.m_EmdBuf[i].m_CountIndex * 2;
		Tmp = tFile.GetData(TmpSize);
		IndexBuff = new Uint16Array(Tmp);
	  		
		if(this.m_EmdBuf[i].m_CountIndex >65535)
		{
			TRACE("Index Too Large......");
		}
		for(m = 0; m<this.m_EmdBuf[i].m_CountIndex; m++)
		{
			if(IndexBuff[m]<0)
			{
				IndexBuff[m] = 0;
			}
			if(IndexBuff[m]>=this.m_EmdBuf[i].m_CountVertex)
			{
				IndexBuff[m] = this.m_EmdBuf[i].m_CountVertex - 1;
			}
		}
	
		//压缩格式的MED没有切空间数据,如果需要进行动态计算 
		

		this.m_EmdBuf[i].m_VertexBuf = gl.createBuffer();  // 创建一个缓冲区对象
		gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_VertexBuf);
		gl.bufferData(gl.ARRAY_BUFFER, TmpBuffVert, gl.STATIC_DRAW );
		gl.bindBuffer( gl.ARRAY_BUFFER,null);


		this.m_EmdBuf[i].m_NormalBuf = gl.createBuffer();  // 创建一个缓冲区对象
		gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_NormalBuf);
		gl.bufferData(gl.ARRAY_BUFFER, TmpBuffNorm, gl.STATIC_DRAW );
		gl.bindBuffer( gl.ARRAY_BUFFER,null);
		
		if(this.m_EmdBuf[i].m_HaveTex === 1)
		{
			this.m_EmdBuf[i].m_TexBuf = gl.createBuffer();  // 创建一个缓冲区对象
			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_TexBuf);
			gl.bufferData( gl.ARRAY_BUFFER, TmpBuffTex, gl.STATIC_DRAW );
			gl.bindBuffer( gl.ARRAY_BUFFER,null);
		}
		else
		{
			this.m_EmdBuf[i].m_ColorBuf = gl.createBuffer();  // 创建一个缓冲区对象
			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_ColorBuf);
			gl.bufferData( gl.ARRAY_BUFFER, TmpBuffColor, gl.STATIC_DRAW );
			gl.bindBuffer( gl.ARRAY_BUFFER,null);
		}
		this.m_EmdBuf[i].m_IndexBuf = gl.createBuffer();  // 创建一个缓冲区对象
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_EmdBuf[i].m_IndexBuf);
		gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, IndexBuff, gl.STATIC_DRAW );
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
	}
	gl.bindBuffer(gl.ARRAY_BUFFER, null);
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

	this.m_CenterPoint.x = (this.m_MinPoint.x + this.m_MaxPoint.x)/2.0;
	this.m_CenterPoint.y = (this.m_MinPoint.y + this.m_MaxPoint.y)/2.0;
	this.m_CenterPoint.z = (this.m_MinPoint.z + this.m_MaxPoint.z)/2.0;

	this.m_BufAABB = gl.createBuffer(); 
	var AABBData = new Float32Array(24);
	AABBData[0] = this.m_MinPoint.x;
	AABBData[1] = this.m_MinPoint.y;
	AABBData[2] = this.m_MinPoint.z;

	AABBData[3] = this.m_MaxPoint.x;
	AABBData[4] = this.m_MinPoint.y;
	AABBData[5] = this.m_MinPoint.z;


	AABBData[6] = this.m_MaxPoint.x;
	AABBData[7] = this.m_MinPoint.y;
	AABBData[8] = this.m_MaxPoint.z;


	AABBData[9] = this.m_MinPoint.x;
	AABBData[10] = this.m_MinPoint.y;
	AABBData[11] = this.m_MaxPoint.z;

	AABBData[12] = this.m_MinPoint.x;
	AABBData[13] = this.m_MaxPoint.y;
	AABBData[14] = this.m_MinPoint.z;


	AABBData[15] = this.m_MaxPoint.x;
	AABBData[16] = this.m_MaxPoint.y;
	AABBData[17] = this.m_MinPoint.z;


	AABBData[18] = this.m_MaxPoint.x;
	AABBData[19] = this.m_MaxPoint.y;
	AABBData[20] = this.m_MaxPoint.z;


	AABBData[21] = this.m_MinPoint.x;
	AABBData[22] = this.m_MaxPoint.y;
	AABBData[23] = this.m_MaxPoint.z;

	gl.bindBuffer( gl.ARRAY_BUFFER, this.m_BufAABB);
	gl.bufferData( gl.ARRAY_BUFFER, AABBData, gl.STATIC_DRAW );
	gl.bindBuffer( gl.ARRAY_BUFFER,null);

	var AABBIndex = new Uint16Array(24);

	AABBIndex[0] = 0;
	AABBIndex[1] = 1;


	AABBIndex[2] = 1;
	AABBIndex[3] = 2;


	AABBIndex[4] = 2;
	AABBIndex[5] = 3;


	AABBIndex[6] = 3;
	AABBIndex[7] = 0;


	AABBIndex[8] = 4;
	AABBIndex[9] = 5;


	AABBIndex[10] = 5;
	AABBIndex[11] = 6;


	AABBIndex[12] = 6;
	AABBIndex[13] = 7;


	AABBIndex[14] = 7;
	AABBIndex[15] = 4;


	AABBIndex[16] = 0;
	AABBIndex[17] = 4;


	AABBIndex[18] = 1;
	AABBIndex[19] = 5;


	AABBIndex[20] = 2;
	AABBIndex[21] = 6;


	AABBIndex[22] = 3;
	AABBIndex[23] = 7;

	this.m_IndexAABB = gl.createBuffer();  // 创建一个缓冲区对象
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_IndexAABB);
	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, AABBIndex, gl.STATIC_DRAW );
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

	if(this.m_CompleteCBFunc !== null)
	{
		this.m_CompleteCBFunc();
	}
}
ReadEmd.prototype.SetCompCB = function(func)
{
	this.m_CompleteCBFunc = func;
}
ReadEmd.prototype.FlcPaly = function(gl)
{
	m_Speed = 1;
	this.m_Delay ++;
	if(this.m_Delay >= m_Speed)
	{
		this.m_Delay = 0;
		this.m_CurFrame ++;

		if(this.m_CurFrame >= this.m_PlayEndFrame)
		{
			if(this.m_AutoRePlay === 1)
			{
				this.m_CurFrame = this.m_PlayBeginFrame;
				//TRACE("RePlay"+this.m_PlayBeginFrame + this.m_PlayEndFrame);
			}
			else
			{
				this.m_PlayFlag = 0;
				this.m_CurFrame = 0;
			}
		}
	}
	else
	{
		return;
	}

	if(this.m_PlayFlag === 0 )
	{
		this.m_CurFrame = 0;
	}
	return;
}

//发出文件读取请求
ReadEmd.prototype.ReadEmd = function(tfileName,TexPath)
{
	this.m_IndexForSel = ReadEmd.m_SubObjIndex;
	ReadEmd.m_SubObjIndex ++;
	this.ReadFlag[0] = 0;
	this.fileName = "" +  tfileName;
	this.m_TexPath = ""+TexPath;
	this.m_GetFile.ReadBinFile(tfileName, this, this.m_SyncTool);
	return;
}

ReadEmd.prototype.SetScale = function(scale)
{
	this.m_Scale.x = this.m_Scale.y = this.m_Scale.z = scale;
}

ReadEmd.prototype.SetPos = function(pos)
{
	this.m_Pos.x = pos.x;
	this.m_Pos.y = pos.y;
	this.m_Pos.z = pos.z;
}

ReadEmd.prototype.GetPos = function()
{
	var pos = new CVector3();
	pos.x = this.m_Pos.x;
	pos.y = this.m_Pos.y;
	pos.z = this.m_Pos.z;
	return pos;
}


ReadEmd.prototype.Rotate = function(rotate)
{
	//在当前基础上旋转n度
	//alert(""+this.m_Rotate.y);
	this.m_Rotate.y += rotate;	
	this.m_Rotate.y = (this.m_Rotate.y % 360);
	//alert(""+this.m_Rotate.y);
	this.m_Direction.x = Math.cos(this.m_Rotate.y * 3.1515926/180.0);
	this.m_Direction.y = 0;
	this.m_Direction.z = -Math.sin(this.m_Rotate.y * 3.1415926/180.0);
	this.m_Direction = this.m_Direction.Normalize();
}

ReadEmd.prototype.SetRotate = function(rotate)
{
	//设置旋转角度绝对量
	//alert("ro");
	this.m_Rotate.y = rotate;
}

ReadEmd.prototype.GetMVP2 = function(tCamera)
{
	var tModel = new CMatrix4();
	var tMVP = new CMatrix4(); 
	tModel.Identity();

	tModel.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	tModel.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);
	tModel.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);
	var viewProjMatrix = tCamera.m_ViewProjMatrix;
	for(var ii = 0; ii<16; ii++)
	{
		tMVP.m[ii] = viewProjMatrix.m[ii];
	}
	tMVP.SyncData();
	tMVP = tModel.Multiply(tMVP);
	return tMVP;
}
ReadEmd.prototype.GetMVP = function(tCamera)
{
	var tModel = new CMatrix4();
	var tMVP = new CMatrix4(); 
	tModel.Identity();
	
	var tmp = new CMatrix4();


	tmp.Identity();
	tmp.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);
	tModel = tModel.Multiply(tmp);


	tmp.Identity();
	tmp.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);
	tModel = tModel.Multiply(tmp);

	

	
	tmp.Identity();
	tmp.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	tModel = tModel.Multiply(tmp);

	
	

	
	
	var viewProjMatrix = tCamera.m_ViewProjMatrix;
	for(var ii = 0; ii<16; ii++)
	{
		tMVP.m[ii] = viewProjMatrix.m[ii];
	}
	tMVP.SyncData();
	tMVP = tModel.Multiply(tMVP);
	return tMVP;
}

ReadEmd.prototype.PlayFlc = function()
{
	if(this.m_SyncTool.IsSync() === true)
	{
		var gl = this.m_GL;
		this.FlcPaly(gl);
	}
}

var fi = 1; 
ReadEmd.prototype.OwnerDraw = function(tCamera)
{
	if(this.m_SyncTool.IsSync() === false)
	{
		TRACE("Geting.....");
		return;
	}
	if(this.m_PlayFlag === 1)
	{
		this.PlayFlc();
	}
	var gl = this.m_GL;


	gl.useProgram(this.m_ShaderProg);
	var ModelMat = new CMatrix4();
	ModelMat.Identity();

	var tmp = new CMatrix4();
	tmp.Identity();
	tmp.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);

	ModelMat = ModelMat.Multiply(tmp);

	tmp.Identity();
	tmp.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);
	ModelMat = ModelMat.Multiply(tmp);

	tmp.Identity();
	tmp.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	ModelMat = ModelMat.Multiply(tmp);
	/*
	ModelMat.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);	
	ModelMat.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);
	
	ModelMat.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	*/
	
	
	

	gl.uniformMatrix4fv(this.u_ModelMatrix, false, ModelMat.m);

	var NorMat = new Float32Array(9);
	//将ModelMat的逆转秩矩阵作为法线矩阵传递给SHADER
	var InvModel = ModelMat.Invert();

	NorMat[0] = InvModel.m[0];
	NorMat[1] = InvModel.m[4];
	NorMat[2] = InvModel.m[8];
	NorMat[3] = InvModel.m[1];
	NorMat[4] = InvModel.m[5];
	NorMat[5] = InvModel.m[9];
	NorMat[6] = InvModel.m[2];
	NorMat[7] = InvModel.m[6];
	NorMat[8] = InvModel.m[10];
	gl.uniformMatrix3fv(this.u_NormalMatrix2, false, NorMat);

	var vMat= tCamera.GetViewMat();
	var mMatrix = new CMatrix4();
	for(var ii = 0; ii<16; ii++)
	{
		mMatrix[ii] = ModelMat.m[ii];
	}
	mMatrix.SyncData();
	var mvMat = mMatrix.Multiply(vMat);
	//alert(""+this.u_MViewMatrix);
	gl.uniformMatrix4fv(this.u_MViewMatrix, false, ModelMat.m);
	var Cmap = tCamera.GetPos();

	

	var InvMat = mvMat.Invert();
	

	
	var tmpEysPos = new Float32Array(3);
	tmpEysPos[0] = Cmap.x;
	tmpEysPos[1] = Cmap.y;
	tmpEysPos[2] = Cmap.z;
	gl.uniform3fv(this.u_EyePos,  tmpEysPos);

	
	NorMat[0] = InvMat.m[0];
	NorMat[1] = InvMat.m[4];
	NorMat[2] = InvMat.m[8];
	NorMat[3] = InvMat.m[1];
	NorMat[4] = InvMat.m[5];
	NorMat[5] = InvMat.m[9];
	NorMat[6] = InvMat.m[2];
	NorMat[7] = InvMat.m[6];
	NorMat[8] = InvMat.m[10];
	gl.uniformMatrix3fv(this.u_NormalMatrix, false, NorMat);		//MV的逆转秩矩阵
	/*

	for(var ii = 0; ii<16; ii++)
	{
		this.m_modelMatrix.m[ii] = ModelMat.m[ii];
	}
	this.m_modelMatrix.SyncData();
	// Calculate the normal transformation matrix and pass it to u_NormalMatrix
	this.m_normalMatrix.Invert(this.m_modelMatrix);
	this.m_normalMatrix.Transpose();
	gl.uniformMatrix4fv(this.u_NormalMatrix, false, this.m_normalMatrix.m);
	*/

	// Calculate the model view project matrix and pass it to u_MvpMatrix
	var viewProjMatrix = tCamera.m_ViewProjMatrix;

	var mvp = new CMatrix4();
	for(var ii = 0; ii<16; ii++)
	{
		mvp.m[ii] = viewProjMatrix.m[ii];
	}
	mvp.SyncData();

	//this.m_mvpMatrix = this.m_mvpMatrix.Multiply(this.m_modelMatrix);
	mvp = ModelMat.Multiply(mvp);


	mvp = this.GetMVP(tCamera);
	//gl.uniformMatrix4fv(this.u_MvpMatrix, false, this.m_mvpMatrix.elements);
	gl.uniformMatrix4fv(this.u_MvpMatrix, false, mvp.m);



	//TRACEMAT("dmvp",this.m_modelMatrix);


	var PosAttr = this.a_Position;
	var NormalAttr = this.a_Normal;
	var TexCoorAttr = this.a_TexCoor;
	var ColorAttr = this.a_Color;

	gl.enableVertexAttribArray(PosAttr);  // Enable the assignment
	gl.enableVertexAttribArray(NormalAttr);  // Enable the assignment 
	gl.enableVertexAttribArray(TexCoorAttr);  // Enable the assignment 
	var i;
	
	for(i = 0; i < this.m_CountObj; i++)
	{

		var tn2 = this.m_EmdBuf[i].m_ObjName.substr(0,10);
		
		if(this.m_EmdBuf[i].m_HaveFlc === 1)
		{
			gl.uniform1f(this.u_HaveFlcFlag,1.0);
			gl.uniformMatrix4fv(this.u_FlcMatrix, false, this.m_EmdBuf[i].m_MatArray[this.m_CurFrame].m);
		}
		else
		{
			gl.uniform1f(this.u_HaveFlcFlag,0.0);
		}
		if(this.m_EmdBuf[i].m_CountIndex < 57600)
		{

			if(this.m_EmdBuf[i].m_HaveTex === 1)	//没有纹理的将来使用颜色绘制
			{
				gl.uniform3fv(this.u_Diffuse, this.m_EmdBuf[i].m_Diffuse);
				gl.uniform3fv(this.u_Ambient,  this.m_EmdBuf[i].m_Ambient);
				gl.uniform3fv(this.u_Specular, this.m_EmdBuf[i].m_Specular);
				
				gl.uniform1f(this.u_Shiness, this.m_EmdBuf[i].m_Shiness);
				gl.uniform1f(this.u_Power, this.m_EmdBuf[i].SpecPower);
				


				gl.activeTexture(gl.TEXTURE0);
				gl.uniform1f(this.u_NoTexFlag, 0.0);
				gl.bindTexture(gl.TEXTURE_2D, this.m_EmdBuf[i].m_TextureID);
				gl.uniform1i(this.u_TexSampler, 0);
				if(this.m_ShowNormalFlag === true)
				{
					
					gl.uniform1f(this.m_ShowNormalHadle,1.0);
					gl.activeTexture(gl.TEXTURE1);
					gl.bindTexture(gl.TEXTURE_2D, this.m_NormapTexID);
					gl.uniform1i(this.m_iNormalSimp, 1);
				}
				else
				{
					TRACE("TESTTTT2222");
					gl.uniform1f(this.m_ShowNormalHadle,0.0);
				}



				gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_VertexBuf);
				gl.vertexAttribPointer(this.a_Position, 3, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable

				gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_NormalBuf);
				gl.vertexAttribPointer(this.a_Normal, 3, gl.FLOAT, false, 0, 0);

				gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_TexBuf);
				gl.vertexAttribPointer(this.a_TexCoor, 2, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable


				gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_EmdBuf[i].m_IndexBuf);
				gl.drawElements(gl.TRIANGLES, this.m_EmdBuf[i].m_CountIndex, gl.UNSIGNED_SHORT, 0);
				//TRACE(""+this.m_EmdBuf[i].m_CountIndex);
			}
		}
		else
		{
			TRACE("ERROR_OBJ:"+this.m_EmdBuf[i].m_ObjName);
		}
		gl.bindBuffer(gl.ARRAY_BUFFER,null);
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null);
	}
	gl.bindBuffer(gl.ARRAY_BUFFER,null);
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null);
	gl.disableVertexAttribArray(PosAttr);  // Disable the assignment
	gl.disableVertexAttribArray(NormalAttr);  // Disable the assignment
	gl.disableVertexAttribArray(TexCoorAttr);  // Enable the assignment 
}


ReadEmd.prototype.DrawOutLine = function(tCamera)
{
	//gl.LineWidth(5.0);
	
	var step = 0.005;
	this.m_Density	= this.m_Density + this.m_DenDir * step;
	//TRACE("ALPHA:"+this.m_DensityHandle);
	if(this.m_Density >= 0.99)
	{
		this.m_DenDir = -1.0;
	}
	if(this.m_Density < 0.8)
	{
		this.m_DenDir = 1.0;
	}
	if(this.m_Complete !== 2)
	{
		return;
	}
	
	var gl = this.m_GL;
	
	gl.enable(gl.BLEND);//首先要启用混合
	gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);//源色和现有色的相加
	gl.enable(gl.CULL_FACE);
	gl.cullFace(gl.CW);

	//TRACE(""+this.m_ShaderOutline);
	gl.useProgram(this.m_ShaderOutline);

	var viewProjMatrix = this.GetMVP(tCamera);
	gl.uniformMatrix4fv(this.m_MvpOutline, false, viewProjMatrix.m);
	
	//增加这两个数据
	//this.m_MVOutline  		= -1;
	//this.m_ProjOutline  	= -1;
 
	var m = new CMatrix4();
	m.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	var mv = tCamera.GetViewMat();
	mv.Multiply(m);

	var proj = new CMatrix4();
	proj.MakePerspective(60.0, this.m_Sspect, 1.0, 8000.0);
	
	gl.uniformMatrix4fv(this.m_ModeOutline, false, m.m);
	gl.uniformMatrix4fv(this.m_MVOutline, false, mv.m);
	gl.uniformMatrix4fv(this.m_ProjOutline, false, proj.m);
	
	
	var PosAttr = this.m_VertOutline;
	var NormalAttr = this.m_NorOutline;
	var TexCoorAttr = this.m_TexCoorOutline;
	var uColor = this.m_ColorOutline;

	// TRACE(""+PosAttr);
	// TRACE(""+TexCoorAttr);
	// TRACE(""+NormalAttr);

	gl.enableVertexAttribArray(PosAttr);  // Enable the assignment
	gl.enableVertexAttribArray(NormalAttr);  // Enable the assignment 
	gl.enableVertexAttribArray(TexCoorAttr);  // Enable the assignment 
	var i;
	
	
	for(i = 0; i < this.m_CountObj; i++)
	{
		var tn = this.m_EmdBuf[i].m_ObjName.substr(0,5);



		if('Bound' !== tn)
		{
			continue;
		}
		if(this.m_EmdBuf[i].m_HaveTex === 1)	//没有纹理的将来使用颜色绘制
		{
			gl.activeTexture(gl.TEXTURE0);
			gl.bindTexture(gl.TEXTURE_2D, this.m_EmdBuf[i].m_TextureID);
			gl.uniform1i(this.m_TexSampOutline, 0);
			gl.uniform1f(this.m_DensityHandle,this.m_Density);

			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_VertexBuf);
			gl.vertexAttribPointer(PosAttr, 3, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable

			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_NormalBuf);
			gl.vertexAttribPointer(NormalAttr, 3, gl.FLOAT, false, 0, 0);

			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_TexBuf);
			gl.vertexAttribPointer(TexCoorAttr, 2, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable


			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_EmdBuf[i].m_IndexBuf);
			gl.drawElements(gl.TRIANGLES, this.m_EmdBuf[i].m_CountIndex, gl.UNSIGNED_SHORT, 0);
		}

		gl.bindBuffer(gl.ARRAY_BUFFER,null);
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null);
	}

	gl.disableVertexAttribArray(PosAttr);  // Disable the assignment
	gl.disableVertexAttribArray(NormalAttr);  // Disable the assignment
	gl.disableVertexAttribArray(TexCoorAttr);  // Enable the assignment 


	gl.cullFace(gl.CCW);
	gl.disable(gl.CULL_FACE);
	//gl.cullFace(gl.BACK);
	gl.disable(gl.BLEND);
	//gl.polygonMode(gl.FRONT, gl.FILL);
	//gl.lineWidth(1.0);
}

//绘制物体的aabb盒
ReadEmd.prototype.DrawAABB = function(tCamera)
{
	if(this.m_SyncTool.IsSync() === false)
	{
		return;
	}
	var gl = GetGLHandle();
	gl.useProgram(this.m_ShaderAABB);
	var mvp = this.GetMVP(tCamera);
	gl.uniformMatrix4fv(this.m_MvpAABB, false, mvp.m);

	gl.enableVertexAttribArray(this.m_VertAABB);  // Enable the assignment
	gl.bindBuffer( gl.ARRAY_BUFFER, this.m_BufAABB);
	gl.vertexAttribPointer(this.m_VertAABB, 3, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable

	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_IndexAABB);
	gl.drawElements(gl.LINES, 24, gl.UNSIGNED_SHORT, 0);

/*
	var tmppos = new CVector4(this.m_CenterPoint.x, this.m_CenterPoint.y, this.m_CenterPoint.z, 1.0);
	var tmppos2 = mvp.MultiplyVector4(tmppos);
	x = tmppos2.x/tmppos2.w;
	y = tmppos2.y/tmppos2.w;
	var canvas = GetCanvasHandle();
	
	x = (x + 1.0)/2.0 * canvas.width;
	y = (y + 1.0)/2.0 * canvas.height;
	//alert("cx = "+ x.toFixed(2)+" cy = "+y.toFixed(2));
*/
}
//每个物体的颜色可能不同，以区分选择每个物体
ReadEmd.prototype.DrawForSel = function(tCamera)
{
	if(this.m_SyncTool.IsSync() === false)
	{
		TRACE("Geting.....");
		return;
	}
	var gl = this.m_GL;
	gl.useProgram(this.m_ShaderSel);

	var ModelMat = new CMatrix4();
	ModelMat.Identity();
	ModelMat.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	ModelMat.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);
	ModelMat.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);


	// 计算MVP矩阵，并传递给u_MvpMatrix
	var viewProjMatrix = tCamera.m_ViewProjMatrix;

	var mvp = new CMatrix4();
	for(var ii = 0; ii<16; ii++)
	{
		mvp.m[ii] = viewProjMatrix.m[ii];
	}
	mvp.SyncData();
	mvp = ModelMat.Multiply(mvp);
	gl.uniformMatrix4fv(this.m_MvpSel, false, mvp.m);
	//TRACEMAT("smvp",ModelMat);


	var PosAttr = this.m_VertSel;

	var uColor = this.m_ColorSel;



	gl.enableVertexAttribArray(PosAttr);  

	var i;
	

	for(i = 0; i < this.m_CountObj; i++)
	{
		if(this.m_EmdBuf[i].m_IsShow === false)
		{
			continue;
		}
		if(this.m_EmdBuf[i].m_CanSel === false)
		{
			continue;
		}

		if(this.m_EmdBuf[i].m_HaveFlc === 1)
		{
			gl.uniform1f(this.m_HaveFlcFlagSel,1.0);
			//TRACE("HAVE FLC........"+this.m_HaveFlcFlagSel);
			gl.uniformMatrix4fv(this.m_FlcMatrixSel, false, this.m_EmdBuf[i].m_MatArray[this.m_CurFrame].m);
		}
		else
		{
			gl.uniform1f(this.m_HaveFlcFlagSel,0.0);
		}

		/*
		var tn = this.m_EmdBuf[i].m_ObjName.substr(0,5);
		var find = 0;
		var color = 0;
		for(m = 0; m < this.m_CountSelObj; m++)
		{
			if(this.m_SelObjColor[m].m_ObjType == tn)
			{
				find = 1;
				color = this.m_SelObjColor[m].m_ObjColor;
				//TRACE("CurType:" + this.m_SelObjColor[m].m_ObjType);
				//TRACE("CurCOLOR:" + color);
			}
		}
		if(find === 0)
		{
			continue;
		}
		
		this.m_uColor[0] = color/255.0;
		*/
		if(this.m_EmdBuf[i].m_HaveTex === 1)	//没有纹理的将来使用颜色绘制
		{
			gl.uniform4fv(this.m_ColorSel, this.m_EmdBuf[i].m_ObjIndexColor);
			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_VertexBuf);
			gl.vertexAttribPointer(PosAttr, 3, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable

			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_EmdBuf[i].m_IndexBuf);
			gl.drawElements(gl.TRIANGLES, this.m_EmdBuf[i].m_CountIndex, gl.UNSIGNED_SHORT, 0);
		}			
		gl.bindBuffer(gl.ARRAY_BUFFER,null);
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null);
	}

	gl.disableVertexAttribArray(PosAttr);  // Disable the assignment
}

//所有的物体为统一的一个颜色
ReadEmd.prototype.DrawToSel = function(tCamera)
{
	var gl = this.m_GL;
	gl.useProgram(this.m_ShaderSel);

	this.m_modelMatrix.Identity();
	this.m_modelMatrix.Translate(this.m_Pos.x, this.m_Pos.y, this.m_Pos.z);
	this.m_modelMatrix.Scale(this.m_Scale.x, this.m_Scale.y, this.m_Scale.z);
	this.m_modelMatrix.Rotate(this.m_Rotate.y, 0.0, 1.0, 0.0);


	// 计算MVP矩阵，并传递给u_MvpMatrix
	var viewProjMatrix = tCamera.m_ViewProjMatrix;

	for(var ii = 0; ii<16; ii++)
	{
		this.m_mvpMatrix.m[ii] = viewProjMatrix.m[ii];
	}
	this.m_mvpMatrix.SyncData();
	this.m_mvpMatrix = this.m_modelMatrix.Multiply(this.m_mvpMatrix);
	gl.uniformMatrix4fv(this.m_MvpSel, false, this.m_mvpMatrix.m);



	var PosAttr = this.m_VertSel;

	var uColor = this.m_ColorSel;



	gl.enableVertexAttribArray(PosAttr);  

	
	IndexToColor(this.m_IndexForSel, this.m_uColor);

	for(var i = 0; i < this.m_CountObj; i++)
	{		
		if(this.m_EmdBuf[i].m_HaveTex === 1)	//没有纹理的将来使用颜色绘制
		{
			gl.uniform4fv(this.m_ColorSel, this.m_uColor);
			gl.bindBuffer( gl.ARRAY_BUFFER, this.m_EmdBuf[i].m_VertexBuf);
			gl.vertexAttribPointer(PosAttr, 3, gl.FLOAT, false, 0, 0);  // Assign the buffer object to the attribute variable

			gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.m_EmdBuf[i].m_IndexBuf);
			gl.drawElements(gl.TRIANGLES, this.m_EmdBuf[i].m_CountIndex, gl.UNSIGNED_SHORT, 0);
		}			
		gl.bindBuffer(gl.ARRAY_BUFFER,null);
		gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null);
	}

	gl.disableVertexAttribArray(PosAttr);  // Disable the assignment
}


ReadEmd.prototype.Play = function()
{
    this.m_PlayFlag = 1;
}
ReadEmd.prototype.AutoRePlay = function()
{
    this.m_AutoRePlay = 1;
}
ReadEmd.prototype.GetSubObjPos = function(subname)
{
	var pos = null;
	var i;	
	for(i = 0; i < this.m_CountObj; i++)
	{
		if(subname === this.m_EmdBuf[i].m_ObjName)
		{
			pos = new CVector3(0,0,0)
			pos.x = this.m_EmdBuf[i].m_VertData[0];
			pos.y = this.m_EmdBuf[i].m_VertData[1];
			pos.z = this.m_EmdBuf[i].m_VertData[2];
			break;
		}
	}
	return pos;
}


ReadEmd.prototype.GetSubObjByIndex= function(index)
{
	var result = null;
	var i;	
	for(i = 0; i < this.m_CountObj; i++)
	{
		if(index === this.m_EmdBuf[i].m_ObjIndex)
		{
			result = this.m_EmdBuf[i];
			break;
		}
	}
	return result;
}

ReadEmd.prototype.GetSubObjByName = function(subname)
{
	var result = null;
	var i;	
	for(i = 0; i < this.m_CountObj; i++)
	{
		if(subname === this.m_EmdBuf[i].m_ObjName)
		{
			result = this.m_EmdBuf[i];
			break;
		}
	}
	return result;
}
//根据名字的前10个字母决定对象是否显示
ReadEmd.prototype.SetShowByLike = function(subname,show)
{
	var i;	
	for(i = 0; i < this.m_CountObj; i++)
	{
		var tmpn = this.m_EmdBuf[i].m_ObjName.substring(0,10);
		if(subname === tmpn)
		{
			this.m_EmdBuf[i].m_IsShow = show;
		}
	}
}

ReadEmd.prototype.GetObjRect = function(name,data)
{
	var i,j,k;	
	var x_max,z_max,x_min,z_min
	data.m_Count = 0;
	for(i = 0; i < this.m_CountObj; i++)
	{
		var tmpn = this.m_EmdBuf[i].m_ObjName.substring(0,6);
		if(name === tmpn)
		{
			//获得该OBJ的最大XZ和最小XZ,Y值不被使用
			x_max = -100000;
			z_max = -100000;
			x_min = 100000;
			z_min = 100000;
			for(j = 0; j<this.m_EmdBuf[i].m_CountVertex; j+=3)
			{
				if(this.m_EmdBuf[i].m_VertData[j*3 + 0]>x_max)
				{
					x_max = this.m_EmdBuf[i].m_VertData[j*3 + 0];
				}
				if(this.m_EmdBuf[i].m_VertData[j*3 + 0]<x_min)
				{
					x_min = this.m_EmdBuf[i].m_VertData[j*3 + 0];
				}
				if(this.m_EmdBuf[i].m_VertData[j*3 + 2]>z_max)
				{
					z_max = this.m_EmdBuf[i].m_VertData[j*3 + 2];
				}
				if(this.m_EmdBuf[i].m_VertData[j*3 + 2]<z_min)
				{
					z_min = this.m_EmdBuf[i].m_VertData[j*3 + 2];
				}
			}
			data.m_Rect[data.m_Count] = new CMyRect();
			data.m_Rect[data.m_Count].x1 = x_min;
			data.m_Rect[data.m_Count].z1 = z_min;
			data.m_Rect[data.m_Count].x2 = x_max;
			data.m_Rect[data.m_Count].z2 = z_max;
			data.m_Count++;
		}
	}
	/*
	for(i = 0; i < data.m_Count; i++)
	{
		TRACE("x1 = " +data.m_Rect[i].x1);
		TRACE("x2 = " +data.m_Rect[i].x2);
		TRACE("z1 = " +data.m_Rect[i].z1);
		TRACE("z2 = " +data.m_Rect[i].z2);
	}
	*/
}
ReadEmd.prototype.SetFlcRange = function(begin, end)
{
	/*
    if(begin<0)
    {
      begin = 0;
    }
    if(begin>this.m_CountFrame)
    {
      begin = this.m_CountFrame;
    }
    
    if(end<0)
    {
      end = 0;
    }
    if(end>this.m_CountFrame)
    {
      end = this.m_CountFrame;
    }
    */
    this.m_CurFrame = begin;
    this.m_PlayBeginFrame = begin;
    this.m_PlayEndFrame = end;
}
